package com.ruoyi.finance.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.finance.domain.ReceiveReconciliationDetail;
import com.ruoyi.finance.dto.ApprovalDto;
import com.ruoyi.finance.mapper.ReceiveReconciliationDetailMapper;
import com.ruoyi.finance.mapper.ReciveOrderMapper;
import com.ruoyi.finance.mapper.ReciveReqMapper;
import com.ruoyi.system.api.domain.finance.ReciveOrder;
import com.ruoyi.system.api.domain.finance.ReciveReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.ReceiveReconciliationMapper;
import com.ruoyi.finance.domain.ReceiveReconciliation;
import com.ruoyi.finance.service.IReceiveReconciliationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 应收核销单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-14
 */
@Service
@Transactional
public class ReceiveReconciliationServiceImpl implements IReceiveReconciliationService 
{
    @Autowired
    private ReceiveReconciliationMapper receiveReconciliationMapper;

    @Autowired
    private ReceiveReconciliationDetailMapper receiveReconciliationDetailMapper;

    @Autowired
    private ReciveReqMapper receiveReqMapper;

    @Autowired
    private ReciveOrderMapper receiveOrderMapper;

    /**
     * 查询应收核销单
     * 
     * @param id 应收核销单主键
     * @return 应收核销单
     */
    @Override
    public ReceiveReconciliation selectReceiveReconciliationById(Long id)
    {
        ReceiveReconciliation receiveReconciliation = receiveReconciliationMapper.selectReceiveReconciliationById(id);
        List<ReceiveReconciliationDetail> list = receiveReconciliationDetailMapper.getDetailByOrderCode(receiveReconciliation.getDocumentCode());
        receiveReconciliation.setReceiveReconciliationDetailList(list);
        return receiveReconciliation;
    }

    /**
     * 查询应收核销单列表
     * 
     * @param receiveReconciliation 应收核销单
     * @return 应收核销单
     */
    @Override
    public List<ReceiveReconciliation> selectReceiveReconciliationList(ReceiveReconciliation receiveReconciliation)
    {
        return receiveReconciliationMapper.selectReceiveReconciliationList(receiveReconciliation);
    }

    /**
     * 新增应收核销单
     * 
     * @param receiveReconciliation 应收核销单
     * @return 结果
     */
    @Override
    public int insertReceiveReconciliation(ReceiveReconciliation receiveReconciliation)
    {
        receiveReconciliation.setCreateTime(DateUtils.getNowDate());
        receiveReconciliation.setCreateBy(SecurityUtils.getUsername());
        String code = "FKHX"+System.currentTimeMillis();
        receiveReconciliation.setDocumentCode(code);
        int num = receiveReconciliationMapper.insertReceiveReconciliation(receiveReconciliation);
        if(num<1){
            throw new ServiceException("系统错误，添加失败，请稍后重试");
        }
        for (ReceiveReconciliationDetail receiveReconciliationDetail:receiveReconciliation.getReceiveReconciliationDetailList()){
            receiveReconciliationDetail.setReconciliationCode(code);
            int count =receiveReconciliationDetailMapper.insertReciveReconciliationDetail(receiveReconciliationDetail);
            if(count<1){
                throw new ServiceException("系统错误，添加失败，请稍后重试");
            }
        }
        return 1;
    }

    /**
     * 修改应收核销单
     * 
     * @param receiveReconciliation 应收核销单
     * @return 结果
     */
    @Override
    public int updateReceiveReconciliation(ReceiveReconciliation receiveReconciliation)
    {
        receiveReconciliation.setModifyTime(DateUtils.getNowDate());
        receiveReconciliation.setModifyBy(SecurityUtils.getUsername());
        int i = receiveReconciliationMapper.updateReceiveReconciliation(receiveReconciliation);
        if(i<1){
            throw new ServiceException("数据错误，修改失败");
        }
        int num = receiveReconciliationDetailMapper.delDetailByCode(receiveReconciliation.getDocumentCode());
        if(num<1){
            throw new ServiceException("数据错误，修改失败");
        }
        for (ReceiveReconciliationDetail receiveReconciliationDetail:receiveReconciliation.getReceiveReconciliationDetailList()){
            receiveReconciliationDetail.setReconciliationCode(receiveReconciliation.getDocumentCode());
            int count =receiveReconciliationDetailMapper.insertReciveReconciliationDetail(receiveReconciliationDetail);
            if(count<1){
                throw new ServiceException("系统错误，添加失败，请稍后重试");
            }
        }
        return 1 ;
    }

    /**
     * 批量删除应收核销单
     * 
     * @param ids 需要删除的应收核销单主键
     * @return 结果
     */
    @Override
    public int deleteReceiveReconciliationByIds(Long[] ids)
    {
        return receiveReconciliationMapper.deleteReceiveReconciliationByIds(ids);
    }

    /**
     * 删除应收核销单信息
     * 
     * @param id 应收核销单主键
     * @return 结果
     */
    @Override
    public int deleteReceiveReconciliationById(Long id)
    {
        return receiveReconciliationMapper.deleteReceiveReconciliationById(id);
    }

    /**
     * 审核
     * @param approvalDto
     * @return
     */
    @Override
    public int approval(ApprovalDto approvalDto) {
        ReceiveReconciliation receiveReconciliation = new ReceiveReconciliation();
        receiveReconciliation.setId(approvalDto.getId());
        receiveReconciliation.setApprover(SecurityUtils.getUsername());
        receiveReconciliation.setApprovalResult(approvalDto.getApprovalResult());
        receiveReconciliation.setApprovalRemark(approvalDto.getApprovalRemark());
        receiveReconciliation.setDocumentStage(1L);
        //处理通过时需要额外修改的数据
        if(approvalDto.getApprovalResult()==2){
            receiveReconciliation.setEffectiveTime(DateUtils.getNowDate());//生效时间
            receiveReconciliation.setIsEffect(1L);//生效状态
        }
        //修改数据
        int num = receiveReconciliationMapper.updateReceiveReconciliation(receiveReconciliation);
        if(num<1){
            throw new ServiceException("系统错误，审核失败");
        }
        //如果请求驳回，直接退出
        if(approvalDto.getApprovalResult()==3){
            return 1;
        }
        //修改应收单及收款单数据
        receiveReconciliation = receiveReconciliationMapper.selectReceiveReconciliationById(approvalDto.getId());
        List<ReceiveReconciliationDetail> list = receiveReconciliationDetailMapper.getDetailByOrderCode(receiveReconciliation.getDocumentCode());
        for(ReceiveReconciliationDetail receiveReconciliationDetail:list){
           if(receiveReconciliationDetail.getSourceDocumentType()==101){
               //应收单
               if(receiveReconciliationDetail.getReqAmount().compareTo(receiveReconciliationDetail.getReconciliationAmount())<0){
                   throw new ServiceException(receiveReconciliationDetail.getTypeName()+":"+receiveReconciliationDetail.getSourceDocumentNo()+"未核销金额小于核销金额，审核失败");
               }
               //核销操作
               ReciveReq receiveReq = new ReciveReq();
               receiveReq.setOrderCode(receiveReconciliationDetail.getSourceDocumentNo());
               receiveReq.setVerMoney(receiveReconciliationDetail.getReconciliationAmount());
               //判断是否核销完
               if(receiveReconciliationDetail.getReqAmount().compareTo(receiveReconciliationDetail.getReconciliationAmount())==0){
                   receiveReq.setOrderStatus(4);
                   receiveReq.setIsClose(1);
               }
                num = receiveReqMapper.updateReciveOrderByOrderCodeOfVerMoney(receiveReq);
               if(num<1){
                   throw new ServiceException("系统错误，修改失败");
               }
           }else{
               if(receiveReconciliationDetail.getOrderAmount().compareTo(receiveReconciliationDetail.getReconciliationAmount())<0){
                   throw new ServiceException(receiveReconciliationDetail.getTypeName()+":"+receiveReconciliationDetail.getSourceDocumentNo()+"未核销金额小于核销金额，审核失败");
               }
               //核销操作
               ReciveOrder reciveOrder = new ReciveOrder();
               reciveOrder.setVerMoney(receiveReconciliationDetail.getReconciliationAmount());
               reciveOrder.setOrderCode(receiveReconciliationDetail.getSourceDocumentNo());
               if(receiveReconciliationDetail.getOrderAmount().compareTo(receiveReconciliationDetail.getReconciliationAmount())==0){
                    reciveOrder.setOrderStatus(4);
                    reciveOrder.setIsClose(1);
               }
               num  = receiveOrderMapper.updateReciveOrderByOrderCodeOfVerMoney(reciveOrder);
               if(num<1){
                   throw new ServiceException("系统错误，修改失败");
               }
           }
        }
        return 1;
    }
}
